package xtgl;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;

public class UserController extends Controller {
	static int g_infNumber = 10;
	static String username = null;
	static String name = null;
	static String position = null;
	public void users() {
		g_infNumber = 10;
		username = null;
		name = null;
		position = null;
		List<Department> depts = Department.dao.find("select * from dept");
		List<Role> roles = Role.dao.find("select * from role");
		setAttr("roles", roles);
		setAttr("depts", depts);
		setAttr("infNumber",g_infNumber);
		render("/xtgl/Users.html");	
	}
	
	public void allUser(){
		if(getPara("name")!=null||getPara("username")!=null||getPara("position")!=null){
			name = getPara("name");
			username = getPara("username");
			position = getPara("position");
		}
		if(getParaToInt("infNumber")!=null){
			g_infNumber = getParaToInt("infNumber");
		}
		if(username==null||name==null||position==null){
			Page<?> users= Db.paginate(getParaToInt("pageNumber", 1), g_infNumber, "SELECT * ","FROM user");
			List<Department> depts = Department.dao.find("select * from dept");
			List<Role> roles = Role.dao.find("select * from role");
			Map<String, Object> map1= new HashMap<String, Object>();
			map1.put("users", users);
			map1.put("depts", depts);
			map1.put("roles", roles);
			List<Object> list1 = new ArrayList<Object>();
			list1.add(map1);
			renderJson(list1);
		} else {

			Page<?> users = Db.paginate(getParaToInt("pageNumber", 1),
					g_infNumber, "SELECT * ",
					"FROM user where username like '%"+ username + "%' and name like '%"+ name
					+ "%' and position like '%"+ position + "%'");
			setAttr("pN", users.getPageNumber());
			setAttr("tP", users.getTotalPage());
			List<Department> depts = Department.dao.find("select * from dept");
			List<Role> roles = Role.dao.find("select * from role");
			Map<String, Object> map1 = new HashMap<String, Object>();
			map1.put("users", users);
			map1.put("depts", depts);
			map1.put("roles", roles);
			List<Object> list1 = new ArrayList<Object>();
			list1.add(map1);
			renderJson(list1);
		}
		
	}


	public void delete() {
		User.dao.deleteById(getParaToInt("id"));
		renderNull();
	}

	public void addUser() throws Exception {
		User user = getModel(User.class, "u");
		List<User> users = User.dao.find("select * from user where username ='"+user.getStr("username")+"'");
		if(users.size()==0){
			Date date = new Date();
			DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String time = format.format(date);
			System.out.println(time);
			user.set("regist_date", time);
			if(user.getDate("hiredate")!=null){
				Date d1 = format.parse(time);
				Date d2 = user.getDate("hiredate");                              
				String str=format.format(d2);
				Date d3 = format.parse(str);
				long diff = d1.getTime() - d3.getTime();
				long year =diff / (1000 * 60 * 60 * 24);
				int y = (int)(year/365);
				user.set("seniority", y);
			}
			int role_id = 0;
			List<Role> roles = Role.dao.find("select * from role");
			for(int i =0;i<roles.size();i++){
				Role role = roles.get(i);
				if(role.getStr("role_name").equals(getPara("role_id"))){
					role_id=role.getInt("role_id");
					break;
				}
			}
			user.set("role_id", role_id);
			user.save();
			//renderJson("success");
			renderText("success");
		}else{
			//renderJson("fail");
			renderText("fail");
		}
	}

	public void update() throws Exception{
		List<User> users = User.dao.find("select * from user where username ='"+getPara("username")+"'");
		User user = User.dao.findById(getParaToInt("id"));
		if(users.size()==0||user.get("username").equals(getPara("username"))){
			user.set("username", getPara("username"));
			user.set("gender", getPara("gender"));
			user.set("name", getPara("name"));
			user.set("age", getParaToInt("age"));
			user.set("dept_id", getParaToInt("dept_id"));	
			user.set("position", getPara("position"));
			user.set("education", getPara("education"));
			user.set("hiredate", getParaToDate("hiredate"));
			user.set("email", getPara("email"));
			user.set("regist_date", getParaToDate("regist_date"));
			int role_id = 0;
			List<Role> roles = Role.dao.find("select * from role");
			for(int i =0;i<roles.size();i++){
				Role role = roles.get(i);
				if(role.getStr("role_name").equals(getPara("role_id"))){
					role_id=role.getInt("role_id");
					break;
				}
			}
			user.set("role_id", role_id);
			user.set("status", getParaToBoolean("status"));
			DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date date = new Date();
			String now = df.format(date);
			Date d1 = df.parse(now);
			Date d2 = user.getDate("hiredate");                              
			String str=df.format(d2);
			Date d3 = df.parse(str);
			long diff = d1.getTime() - d3.getTime();
			long year =diff / (1000 * 60 * 60 * 24);
			int y = (int)(year/365);
			user.set("seniority", y);
			user.update();
			renderText("success");
		}else{
			renderText("fail");
		}
	}
	public void fanye(){
		g_infNumber=getParaToInt("infNumber");
		renderNull();
	}
	
	public void getInfNumber(){
		renderJson(g_infNumber);
	}
}